package hello;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

import connections.BTSerialConnection;
import connections.Connection;
import connections.ConnectionListener;

public class HelloBTPhysComp extends MIDlet {

	PhysCompExampleCanvas myCanvas;

	public void startApp() {
		myCanvas = new PhysCompExampleCanvas();
		Display.getDisplay(this).setCurrent(myCanvas);
	}

	public void pauseApp() {
	}

	public void destroyApp(boolean unconditional) {
		myCanvas.kill();
	}

}

/**
 * @author Dan O'Sullivan The usual three bytes in two bytes back physical computing example
 */
class PhysCompExampleCanvas extends Canvas implements ConnectionListener {
	public int pot = 22;

	public int light = 15;

	public int button = 62;

	String textSoFar = "";

	byte xpos = 120;

	BTSerialConnection myBTSerial;

	String feedback = "none";

	PhysCompExampleCanvas() {
		myBTSerial = new BTSerialConnection(this, "00A096179A85");
		myBTSerial.addListener(this, (char) 10);
		myBTSerial.send(xpos);
	}

	public void incoming(Connection _c, byte[] _input) {
		String inputAsString = new String(_input);
		System.out.println("new" + inputAsString);
		String[] intsAsStrings = Connection.split(inputAsString, ',');
		if (intsAsStrings != null && intsAsStrings.length > 2) {
			try {
				light = Integer.parseInt(intsAsStrings[0].trim());
				light = Math.min(255, Math.max(0, (light - 300) / 3));
				pot = Integer.parseInt(intsAsStrings[1].trim()) / 4;
			
				button = Integer.parseInt(intsAsStrings[2].trim());
				myBTSerial.clearInput();
				myBTSerial.send(xpos);
			} catch (NumberFormatException e) {
				System.out.println("bad number");
			}

			repaint();
		}
	}

	public void feedback(Connection _c, String s) {
		feedback = s;
		repaint();
	}

	public void connectionMade(Connection _c, String _address) {
		feedback = "Ya Boy: " + _address;

	}

	public void kill() {
		myBTSerial.disconnect();
	}

	
	
	public void paint(Graphics g) {
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(light,255,0);
		if (button == 1) {
			g.drawRect(pot, 50, 20, 20);
		} else {
			g.fillRect(pot, 50, 20, 20);

		}
		//g.setColor(0, 255, 0);
		//g.fillRect(xpos, 50, 10, 10);
		g.setColor(0, 255, 0);
		g.drawString("pot" + pot + " lite" + light + " btn" + button, 0, 70, 0);
		g.drawString("feedback" + feedback, 0, 90, 0);

	}

	public void keyRepeated(int keyCode) {
		movit(keyCode);

	}

	public void movit(int keyCode) {
		myBTSerial.send(xpos);
		// connectionThread.send( xpos);
		switch (keyCode) {

		case 52:// 4
			xpos--;
			break;
		case -3:// left
			xpos--;
			break;
		case -4:// right
			xpos++;
			break;
		case 54:// 6
			xpos++;
			break;
		}
		repaint();
	}

	public void keyPressed(int keyCode) {
		myBTSerial.send(xpos);
		movit(keyCode);
		// connectionThread.send(xpos);
	}

}
